﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace suirui.ZhuMu.Model
{
    public class QueryPage
    {
        public static Task<List<T>> getPageData<T, TKey>(DbContext db,
                                                         Expression<Func<T, dynamic>> select,
                                                         Expression<Func<T, bool>> where,
                                                         Expression<Func<T, TKey>> order,
                                                         int pageIndex, int pageSize, out int Total, bool isAsc = true) where T : class
        {
            List<T> tList = new List<T>();
            Total = db.Set<T>().Where(where).Count();
            return Task.Run(() =>
            {                
                List<dynamic> list = null;
                if (isAsc)
                    list = db.Set<T>().Where(where).OrderBy(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                else
                    list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                                
                foreach (var obj in list)
                    tList.Add(obj);

                return tList;
            });
            //return tList;
        }
    }
}
